﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;

namespace BuiHongHai_NguyenThiNham_DaoCongDuc
{
    class Program
    {
        static void Main(string[] args)
        {
            menu thucdon = new menu();            
            thucdon.hienthiMENU();
        }
    }

    public class nguoi
    {
        private string hoten;
        private string namsinh;

        public nguoi()
        {
            hoten = "";
            namsinh = "";
        }

        public nguoi(string ten, string n_sinh)
        {
            hoten = ten;
            namsinh = n_sinh;
        }

        public string get_hoten()
        {
            return hoten;
        }

        public string get_namsinh()
        {
            return namsinh;
        }

        public void set_hoten(string ht)
        {
            hoten = ht;
        }
        public void set_namsinh(string ns)
        {
            namsinh = ns;
        }       
    }

    public class sinhvien : nguoi
    {
        //du lieu
        private string MSSV;
        private int diem_toan;
        private int diem_ly;
        private int diem_hoa;

        //phuong thuc
        public sinhvien()
        {
            MSSV = "";
            diem_toan = 0;
            diem_ly = 0;
            diem_hoa = 0;
        }
        
        //ham set
        public void set_mssv(string _mssv)
        {
            MSSV = _mssv;
        }
        public void set_dtoan(int _dtoan)
        {
            diem_toan = _dtoan;
        }
        public void set_dly(int _dly)
        {
            diem_ly = _dly;
        }
        public void set_dhoa(int _dhoa)
        {
            diem_hoa = _dhoa;
        }

        //ham get
        public string get_mssv()
        {
            return MSSV;
        }
        public int get_dtoan()
        {
            return diem_toan;
        }
        public int get_dly()
        {
            return diem_ly;
        }
        public int get_dhoa()
        {
            return diem_hoa;
        }

        public void nhap()
        {
            Console.Write("Nhap ho ten: ");
            set_hoten(Console.ReadLine());
            Console.Write("Nhap nam sinh: ");
            set_namsinh(Console.ReadLine());
            Console.Write("Nhap mssv: ");
            MSSV = Console.ReadLine();
            Console.Write("Nhap diem toan: ");
            diem_toan = Convert.ToInt16(Console.ReadLine());
            Console.Write("Nhap diem ly: ");
            diem_ly = Convert.ToInt16(Console.ReadLine());
            Console.Write("Nhap diem hoa: ");
            diem_hoa = Convert.ToInt16(Console.ReadLine());
        }

        public void hienthi()
        {
            Console.WriteLine("MSSV:\t" + MSSV);
            Console.WriteLine("Ho ten:\t" + get_hoten());
            Console.WriteLine("Nam sinh:\t"+ get_namsinh());
            Console.WriteLine("Diem Toan:\t" + diem_toan);
            Console.WriteLine("Diem Ly:\t" + diem_ly);
            Console.WriteLine("Diem Hoa:\t" + diem_hoa);
            Console.WriteLine("_______________");
            int dtb=(diem_toan+diem_ly+diem_hoa)/3;
            Console.WriteLine("Diem trung binh: " + dtb);
            if (dtb < 5) Console.WriteLine("Xep loai hoc luc yeu");
            else if(dtb>=5 && dtb <=7) Console.WriteLine("Xep loai hoc luc trung binh");
            else if(dtb > 7 && dtb <= 8) Console.WriteLine("Xep loai hoc luc kha");
            else if (dtb > 8 && dtb <= 10) Console.WriteLine("Xep loai hoc luc gioi");
            Console.WriteLine();
        }
    }

    public class menu
    {
    //DU LIEU
        public int soluongsv;
        public sinhvien[] sv;

    //CAU LENH
        public void hienthiMENU()
        {
        	Console.CursorTop = 5;
        	Console.WriteLine("                            QUAN LY DANH SACH SINH VIEN       ");
            Console.WriteLine("                   |=========================================|");
            Console.WriteLine("                   |            An phim 1: Nhap              |");
            Console.WriteLine("                   |            An phim 2: Tim kiem          |");
            Console.WriteLine("                   |            An phim 3: Luu               |");
            Console.WriteLine("                   |            An phim 4: Hien thi          |");
            Console.WriteLine("                   |            An phim ESC: Thoat           |");
            Console.WriteLine("                   |=========================================|");
            Console.Write("                    Vui long an phim : ");
            ConsoleKeyInfo info = Console.ReadKey();
            Console.WriteLine();
            switch (info.Key)
                {
                    case ConsoleKey.D1:
                        {
                            Console.Clear();       
                			Console.Write("Nhap so luong sinh vien: ");
                			soluongsv = Convert.ToInt32(Console.ReadLine());
                			sv = new sinhvien[soluongsv];
                			//Nhap tung thanh vien
               				 for (int i = 0; i < soluongsv; i++)
                		{
                   			 Console.WriteLine("====================================================");
                   			 Console.WriteLine("Nhap thong tin cho sinh vien thu " + (i + 1));
                   			 sv[i] = new sinhvien();
                    		 sv[i].nhap();
                		}
                			 //Thong bao nhap xong
                			
               				 Console.WriteLine("\nKet thuc !");
              			  	 delay();
                             Console.Clear();
                             hienthiMENU();
                            
                        }
                		break;
                	case ConsoleKey.D2:
                        {	
                			
                           	 //ham tim kiem
                			 Console.Clear();
                			 hienthitimkiem();
                        }
                	    break;
                	case ConsoleKey.D3:
                        {
                       		//ham save                
                            save();
                            hienthiMENU();
                        }
                	    break;
                	case ConsoleKey.D4:
                        {
                           //ham hien thi thong tin
                           kiem_tra_du_lieu_da_nhap();
                           hienthithongtin();
                        }
                	    break;
                    case ConsoleKey.Escape:
                        {
                           tambiet();
                           //thoat
                        }
                	    break;
                    default:
                        Console.Clear();
                        nhapsai();
                        break;
                        
            }
        }
        //HIEN THI TIM KIEM
        public void hienthitimkiem()
        {
            Console.Clear();
            Console.CursorTop = 5;
            Console.WriteLine("                              CHUONG TRINH TIM KIEM         ");
            Console.WriteLine("                  |==========================================|");
            Console.WriteLine("                  |             Nhan 1: Theo MSSV            |");
            Console.WriteLine("                  |             Nhan 2: Theo Ten             |");
            Console.WriteLine("                  |             Nhan 3: Diem trung binh      |");
            Console.WriteLine("                  |             Nhan 4: Theo xep loai        |");
            Console.WriteLine("                  |             Nhan 5: Quay ve trang chu    |");
            Console.WriteLine("                  |             Nhan ESC de Thoat            |");
            Console.WriteLine("                  |==========================================|");
            ConsoleKeyInfo info = Console.ReadKey();
            switch (info.Key)
            {	
            case ConsoleKey.D1:
            {
                   //tim kiem theo mssv
                	Console.Write("\n\nNhap MSSV de tim kiem: ");
	                string temp = Console.ReadLine();
	                for (int i = 0; i < soluongsv; i++)
                	{	
                    if (sv[i].get_mssv() == temp)
                    {
                        sv[i].hienthi();
                    }
            }
                delay();
                hienthitimkiem();
           		}
            	break;
            		
            case ConsoleKey.D2:
            	{
                	//tim kiem theo ten
                	Console.Write("\n\nNhap Ten de tim kiem: ");
                	string temp = Console.ReadLine();
                	Console.WriteLine();
                	for (int i = 0; i < soluongsv; i++)
               	 {
                    if (sv[i].get_hoten() == temp)
                    {
                        sv[i].hienthi();
                    }
                }
      
                delay();
                hienthitimkiem();
           		}
            	break;
            case ConsoleKey.D3:
            {
                //tim kiem sv theo diem trung binh
                Console.Write("\n\nNhap diem trung binh de tim kiem: ");
                double temp = Convert.ToDouble(Console.ReadLine());
                Console.WriteLine();
                for (int i = 0; i < soluongsv; i++)
            {
                    if ((sv[i].get_dtoan() + sv[i].get_dly() + sv[i].get_dhoa())/3 == temp)
                    {
                        sv[i].hienthi();
                    }
            }
                delay();
                hienthitimkiem();
            }
            	break;
            case ConsoleKey.D4:
           	{	
                //tim kiem sinh vien theo xep loai
                timkiemtheoxeploai();
                delay();
            }
            	break;
            case ConsoleKey.D5:
            {
                //back
                Console.Clear();
                hienthiMENU();
            }
            	break;
            case ConsoleKey.Escape:
            {
                //thoat
                tambiet();
            }
            break;
            default:
            	nhapsai();            
            	break;
            
         }
       }

        //SAVE
        public void save()
        {
            string filePath = "D:\\k54.txt";
                          
                using (StreamWriter sw = File.CreateText(filePath))
                {
                    for (int i = 0; i < soluongsv; i++)
                    {
                        sw.WriteLine(sv[i].get_hoten());
                        sw.WriteLine(sv[i].get_mssv());
                        sw.WriteLine(sv[i].get_namsinh());
                        sw.WriteLine(sv[i].get_dtoan());
                        sw.WriteLine(sv[i].get_dly());
                        sw.WriteLine(sv[i].get_dhoa());
                        sw.WriteLine("=================================================");
                    }
                }
                
                Console.WriteLine("Du lieu da duoc ghi vao: " + filePath);
                delay();
        }

        //Nap file so lieu vap bo nho
        public void nap_file_so_lieu_vao_bo_nho(string filePath)
        {
            if (File.Exists(filePath))
            {
                int i = 0;
                sv = new sinhvien[5000];
                //su dung StreamReader de doc file
                using (StreamReader sr = File.OpenText(filePath))
                {
                    string hoten, mssv, ns, dtoan, dly, dhoa, temp = "";
                    while
                       ((hoten = sr.ReadLine()) != null &&
                        (mssv = sr.ReadLine()) != null &&
                        (ns = sr.ReadLine()) != null &&
                        (dtoan = sr.ReadLine()) != null &&
                        (dly = sr.ReadLine()) != null &&
                        (dhoa = sr.ReadLine()) != null &&
                        (temp = sr.ReadLine()) != null)
                    {
                        sv[i] = new sinhvien();
                        sv[i].set_hoten(hoten);
                        sv[i].set_mssv(mssv);
                        sv[i].set_namsinh(ns);
                        sv[i].set_dtoan(int.Parse(dtoan));
                        sv[i].set_dly(int.Parse(dly));
                        sv[i].set_dhoa(int.Parse(dhoa));
                        i++;
                    }
                    soluongsv = i;
                    Console.WriteLine("Nap file thanh cong! Da nap vao bo nho {0} sinh vien.", i);
                    Console.ReadKey();
                }
            }
            else
            {
                Console.Clear();
                Console.WriteLine("File khong ton tai vui long thao tac lai !"); ;
                Console.ReadKey();
            }
        }

        public void kiem_tra_du_lieu_da_nhap()
        {
            if (soluongsv == 0)
            {
                Console.Clear();
                Console.CursorTop = 5;
                Console.WriteLine("                          DU LIEU CHUA DUOC NAP VAO BO NHO \n");
                Console.WriteLine("                          Nhan phim 1: nhap du lieu tu file ");
                Console.WriteLine("                          Nhan phim 2: bo qua");
                Console.Write("Chon: \n");
                ConsoleKeyInfo info = Console.ReadKey();
                switch (info.Key)
                {
                    case ConsoleKey.D1:
                        Console.Write("\t Nhap ten file can mo : ");
                        string filePath = Console.ReadLine();
                        filePath = "D:\\" + filePath;
                        //doc file
                        nap_file_so_lieu_vao_bo_nho(filePath);
                        break;
                    case ConsoleKey.D2:
                        break;
                    default:
                        nhapsai();
                        break;
                }
            }
        }

        //HIEN THI THONG TIN
        public void hienthithongtin()
        {
            Console.Clear();
            Console.CursorTop = 5;
            Console.WriteLine("                     Chon kieu hien thi thong tin             ");
            Console.WriteLine("                  |==========================================|");
            Console.WriteLine("                  |   Nhan 1: Theo diem trung binh tang dan  |");
            Console.WriteLine("                  |   Nhan 2: SV co diem ca 3 mon >= 5       |");
            Console.WriteLine("                  |   Nhan 3: Quay lai trang truoc           |");
            Console.WriteLine("                  |   Nhan ESC de Thoat                      |");
            Console.WriteLine("                  |==========================================|");
            ConsoleKeyInfo info = Console.ReadKey();
            Console.WriteLine();
            switch(info.Key)
            {
           	case ConsoleKey.D1:
            {
                //Ham hien thi theo diem trung binh tang dan
                hienthi_sv_theo_dtb_tangdan();
                delay();
                hienthithongtin();
            }
            break;
            case ConsoleKey.D2:
            {
                //Ham hien thi sinh vien co diem ca 3 mon >= 5
                hienthi_sv_co_diem_3_mon_lonhon_hoac_bang5();
                delay();
                hienthithongtin();
            }
            break;
            case ConsoleKey.D3:
            {
                Console.Clear();
                hienthiMENU();
            }
            break;
           case ConsoleKey.Escape:
            {
                tambiet();
                //thoat
            }
            break;
           default:
              nhapsai();
              break;
            
        }
        }

        //Ham hien thi theo diem trung binh tang dan
        public void hienthi_sv_theo_dtb_tangdan()
        {
            int i, j;
            sinhvien[] tempsv = new sinhvien[soluongsv];
            sinhvien temp = new sinhvien();
            tempsv = sv;
            for (i = 0; i < soluongsv; i++)
            {
                double dtb_i = (sv[i].get_dtoan() + sv[i].get_dly() + sv[i].get_dhoa()) / 3;
                for (j = i; j < soluongsv; j++)
                {
                    double dtb_j = (sv[j].get_dtoan() + sv[j].get_dly() + sv[j].get_dhoa()) / 3;
                    if (dtb_i > dtb_j)
                    {
                        temp = tempsv[i];
                        tempsv[i] = tempsv[j];
                        tempsv[j] = temp;
                    }
                }
            }
            Console.WriteLine("DANH SACH SINH VIEN:");
            for (i = 0; i < soluongsv; i++)
            {
                tempsv[i].hienthi();
            }
            delay();
            hienthithongtin();
        }

        //Ham hien thi sinh vien co diem ca 3 mon >=5
        public void hienthi_sv_co_diem_3_mon_lonhon_hoac_bang5()
        {
            for (int i = 0; i < soluongsv; i++)
            {
                if ((sv[i].get_dly() >= 5) && (sv[i].get_dtoan() >= 5) && (sv[i].get_dhoa() >= 5))
                {
                    sv[i].hienthi();
                    Console.WriteLine();
                }
            }
        }

        //Tim kiem sinh vien theo xep loai        
        public void timkiemtheoxeploai()
        {
            Console.Clear();
            Console.WriteLine("Nhan 1: Yeu");
            Console.WriteLine("Nhan 2: Trung binh");
            Console.WriteLine("Nhan 3: Kha");
            Console.WriteLine("Nhan 4: Gioi");
            Console.WriteLine("Nhan 5: De quay lai trang truoc");
            Console.WriteLine("Nhan ESC de thoat");
            Console.WriteLine("__________________________________________________\n");
            ConsoleKeyInfo info = Console.ReadKey();
            Console.WriteLine();
            switch(info.Key)
            {
            //Tim kiem sinh vien co hoc luc yeu
           case ConsoleKey.D1:
            {
                for (int i = 0; i < soluongsv; i++)
                {
                    double diemtrungbinh = (sv[i].get_dtoan() + sv[i].get_dly() + sv[i].get_dhoa())/3;
                    if (diemtrungbinh < 5)
                    {
                        sv[i].hienthi();
                        Console.WriteLine();
                    }
                }
            }
            break;
            //Tim kiem sinh vien co hoc luc trung binh
           case ConsoleKey.D2:
            {
                for (int i = 0; i < soluongsv; i++)
                {
                    double diemtrungbinh = (sv[i].get_dtoan() + sv[i].get_dly() + sv[i].get_dhoa()) / 3;
                    if (diemtrungbinh >= 5 && diemtrungbinh <= 7)
                    {
                        sv[i].hienthi();
                        Console.WriteLine();
                    }
                }
            }
            break;
            //Tim kiem sinh vien co hoc luc kha
            case ConsoleKey.D3:
            {
                for (int i = 0; i < soluongsv; i++)
                {
                    double diemtrungbinh = (sv[i].get_dtoan() + sv[i].get_dly() + sv[i].get_dhoa()) / 3;
                    if (diemtrungbinh > 7 && diemtrungbinh <= 8)
                    {
                        sv[i].hienthi();
                        Console.WriteLine();
                    }
                }
            }
            break;
            //Tim kiem sinh vien co hoc luc gioi
            case ConsoleKey.D4:
            {
                for (int i = 0; i < soluongsv; i++)
                {
                    double diemtrungbinh = (sv[i].get_dtoan() + sv[i].get_dly() + sv[i].get_dhoa()) / 3;
                    if (diemtrungbinh >=8 && diemtrungbinh <= 10)
                    {
                        sv[i].hienthi();
                        Console.WriteLine();
                    }
                }
            }
            break;
            case ConsoleKey.D5:
            {
                //back
                hienthitimkiem();
            }
            break;
            case ConsoleKey.Escape:
            {
                tambiet();
            }
            break;
            default:
            nhapsai();
            //Sau khi tim kiem xong
            Console.WriteLine("_____________________________________________________________________");
            delay();            
            timkiemtheoxeploai();
            break;
        }
        }
           
        //Delay
        public void delay()
        {
            Console.WriteLine("\nNhan phim bat ki!");
            Console.ReadKey();       
        }
        
        //Ham tam biet
        public void tambiet()
        {	
            Console.Clear();
            Console.CursorTop = 5;
            Console.WriteLine("                                     HEN GAP LAI !");
            
        }

        //nhap bao nhap sai
        public void nhapsai()
        {
            Console.Clear();        
            Console.WriteLine("Phim bam khong hop le, vui long thu lai");
            hienthiMENU();
        }

        
    }
}


